CN.record = "个班级";
var swfu;
var msg=CN;
var ufile = {fid:0,filepath:"",name:""};
var userfile = {fid:0,selectfileid:0,name:"",path:"",target:""};
var objectModel = {edit_status:"create",uid:site.uid,status:1};
$(function(){
	init();
	mpage.modalfactory = function(data,position){//生成列表内容 //TODO 需要变化的地方
        $(".tablist").empty();
        tablist.position = position;//设置数据所在位置
        var tdValue =  "",
            rowclass = "",
            td = function(arg,title){
        		if(!arg)arg="-";
	        	if(title){
	        		return "<td title='"+title+"'>"+arg+"</td>";           
	        	}else{
	        		return "<td>"+arg+"</td>";
	        	}
            },
            setStatus = function(arg){
            	var sta = ["暂停","可用"];
            	var cla = ["label-important","label-success"];
            	return "<span class='label "+cla[arg]+"'>"+sta[arg]+"</span>";
            };
            setButton = function(data){
            	var btns = '<div class="btn-group" data-toggle="buttons-radio">';
            	btns += "<a class='btn btn-info btn-mini' onclick='del(this)' data-name='"+data.name+"' data-id='"+data.gid+"'>移除</a>";
            	btns += "</div>";
            	return btns;
            };
        for ( var element in data) {
        	var kk =data[element],
             	u_btn = '<button class="btn btn-info btn-mini" onclick="get(this)" type="button" data-id="'+kk.gid+'">'+kk.name+'</button>';
             	tdValue =
             	td(kk.gid)+
            	td(kk.year)+ 
            	td(u_btn)+
            	td(kk.desc)+ 
            	td(kk.wxgroup)+
            	td(setStatus(kk.status))+
             	td(setButton(kk));
               	rowclass = element%2;//行样式
               $(".tablist").append("<tr class='tr"+rowclass+"'>"+tdValue+"</tr>");
        }
    };
	mpage.init();
}); 

function init(){
	init_dialog();
	init_btn();
}

init_btn = function(){		
	$(".setstatus").click(function(){
		objectModel.status = $(this).attr("key");
	});	
}

init_dialog = function(){
	$("#dialog_create").click(function(){//create & open dialog
		objectModel.edit_status = "create";
		open_dialog();
	});
	$("#dialog_save").click(function(){
		$(this).attr("disabled",true);
		$(".dialogmsg").text("执行操作中...");
		$("#form2 input").each(function(){
			var id = $(this).attr("id");
			if(id)
				objectModel[id] =  $(this).val();
		});
		objectModel.uid = site.uid;
		objectModel.url = site.web+site.module+"/"+objectModel.edit_status;//包含create/update操作
						
		if(objectModel.name==""){
			$(".dialogmsg").text("班级名称不能为空!");
			return;
		}
		if(objectModel.year==""){
			$(".dialogmsg").text("年份不能为空!");
			return;
		}		
		if(!validate(objectModel.year,0)){
			$(".dialogmsg").text("年份请录入数字");
			return;
		}	
		complete = function(data){
			done_method = function(){
				$('#myModal').modal('hide');
				mpage.init();
			};
			if(data.code==1){
				$(".dialogmsg").text("操作成功!");
				setTimeout(done_method, 2000);
				
			}else{
				$('#myModal').modal('hide');
			}
			
		}
		G(objectModel,complete);
	});	
	
	$(".canceldialog").click(function(){//cancel
		clear_dialog();
		$('#myModal').modal('hide');
	});
	$(".canceldialog2").click(function(){//cancel
		$('#myModal2').modal('hide');
		$('#myModal').modal('show');
	});
}

clear_dialog = function(){
	$("#form2 input[type=reset]").click();
	$("#dialog_save").attr("disabled",false);
	$(".dialogmsg").text("");
}

open_dialog = function(data){
	clear_dialog();
	switch(objectModel.edit_status){
		case "create":
			$(".dialog_status").text("新增");
			break;
		case "update":
			$(".dialog_status").text("更新");

			$.each(data, function(index, value) {
				$("#"+index).val(value);
			});
			objectModel = data;
			objectModel.edit_status = "update";
			objectModel.uid = site.uid;
			
			$(".setstatus").each(function(kev,value){
				if($(this).attr("key")==objectModel.status){
					$(this).click();
				}
			});
			break;
	}
	$('#myModal').modal('show');
}

get = function(arg){
	var id = $(arg).attr("data-id");
	cb = function (data){
		if(data.code==1){
			objectModel.edit_status = "update";
			open_dialog (data.data);
		}else{
			alert("ERROR grade 404");
		}
	}
	var url = site.web+site.module+"/get";
	var json = {url:url,gid:id,uid:site.uid};
	G(json,cb);
}

del = function(arg){
	var name =  $(arg).attr("data-name");
	if(confirm("确认移除"+name+"?")==false)return;
	var id = $(arg).attr("data-id");
	cb = function (data){
		mpage.init();
	}
	var url = site.web+site.module+"/del";
	var json = {url:url,gid:id,uid:site.uid};
	G(json,cb);
}


